//
//  Set Form Values of Current Webpage.m
//  Set Form Values of Current Webpage
//
//  Created by itod on 3/30/07.
//  Copyright 2007 Todd Ditchendorf. All rights reserved.
//

#import "Set Form Values of Current Webpage.h"

@implementation Set_Form_Values_of_Current_Webpage

#pragma mark -
#pragma mark AbstractMethods

- (NSString *)javaScriptSource
{
	NSString *elementIdentifier = [self objectForParameterKey:@"elementIdentifier"];
	NSString *formIdentifier	= [self objectForParameterKey:@"formIdentifier"];
	NSString *elementValue		= [self objectForParameterKey:@"elementValue"];
	elementValue = (elementValue) ? elementValue : @"";
	
	
	NSMutableString *jsSource = [NSString stringWithContentsOfFile:[self javaScriptFilePath]];

	if ([self isIdSelected]) {
		jsSource = [NSString stringWithFormat:jsSource, elementIdentifier, elementValue];
	} else {
		jsSource = [NSString stringWithFormat:jsSource, formIdentifier, elementIdentifier, elementValue];
	}
	jsSource = [NSMutableString stringWithString:jsSource];
	
	[jsSource replaceOccurrencesOfString:@"\"" withString:@"\\\"" options:nil range:NSMakeRange(0, [jsSource length])];

	return jsSource;
}


- (NSString *)javaScriptFilePath
{
	NSString *filename = ([self isIdSelected]) ? @"SetValueOfElementWithId" : @"SetValueOfElementWithName";
	
	NSBundle *bundle = [NSBundle bundleForClass:[self class]];
	return [bundle pathForResource:filename ofType:@"js" inDirectory:@"JavaScript"];
}

@end
